//
//  LYOrzViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/11.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYOrzViewController.h"
#import "LYOrzWebViewController.h"
#import "LYOrzCell.h"
#import "LYSearchBar.h"
#import "LYScreenBar.h"
#import "LYCityViewController.h"
#import "LYChooseListView.h"
#import "LYSubjectListDBTool.h"



@interface LYOrzViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *models;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) LYSearchBar *searchBar;

@property (weak, nonatomic) LYScreenBar *screenBar;
@property (nonatomic, weak) LYChooseListView *chooseView;

@end

@implementation LYOrzViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.titleView = self.searchBar;
    
    [self setUpScreenBar];
    
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - 懒加载
- (LYSearchBar *)searchBar{
    
    if (_searchBar == nil) {
        LYSearchBar *searchBar = [[LYSearchBar alloc] init];
        searchBar.frame = CGRectMake(0, 0, CGFLOAT_MAX, 30);
        searchBar.placeholder = @"搜索科目、老师、课程、机构";
        [searchBar addTarget:self action:@selector(textfieldChange:) forControlEvents:UIControlEventEditingChanged];
        
        _searchBar = searchBar;
    }
    return _searchBar;
}



#pragma mark - 初始化
/**
 *  初始化筛选栏
 */
- (void)setUpScreenBar{
    
    LYScreenBar *screenBar = [LYScreenBar viewForNib];
    
    screenBar.frame = CGRectMake(0, 0, LYScreenWidth, 40);
    self.screenBar = screenBar;
    
    __weak typeof(self) weakSelf = self;
    __weak typeof(screenBar) weakScreenBar = screenBar;
    [self.view addSubview:screenBar];
    screenBar.block = ^(SceenType type){
        
        switch (type) {
            case SceenTypeSub:
                
                if (weakSelf.chooseView) {
                    [weakSelf.chooseView dismiss];
                }else{
                    
                    weakSelf.chooseView = [LYChooseListView showOnView:weakSelf.tableView];
                    weakSelf.chooseView.block = ^(NSString *title){
                        if (title) {
                            weakScreenBar.subTitle = title;
                            [weakSelf.tableView.mj_header beginRefreshing];
                        }
//                        [weakScreenBar backToNormal];
                    };
                }
                break;
                
                
            case SceenTypeEva:
            {
                [weakSelf.chooseView dismiss];
                [self showPickImageAlertView];
            }
                break;
                
            case SceenTypeDistance:
            {
                [weakSelf.chooseView dismiss];
                
                LYCityViewController *cityVC = [[LYCityViewController alloc] init];
                
                cityVC.block = ^(NSString *cityName, NSInteger cityID){
                    
                    weakScreenBar.nearByTitle = cityName;
                    weakScreenBar.area_id = cityID;
                    [weakSelf.tableView.mj_header beginRefreshing];
                };
                
                [self.navigationController pushViewController:cityVC animated:YES];
            }
                break;
                
            default:
                break;
        }
    };
    
}




#pragma mark - 网络请求

/**
 *  请求新数据
 */
- (void)loadNewData{
    
    LYConditionModel *requestModel = [self getRequestModelWithPage:@"1"];
    
    [LYRequestTool getLocalTeacherAndOrganizationWithcontionModel:requestModel success:^(NSArray<LYPersonalInfo *> *models) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        self.models = [models mutableCopy];
        [self.tableView reloadData];
        self.page = 1;
        
        
    } failure:^(NSString *code, NSString *message) {
        
        [self.tableView.mj_header endRefreshing];
    }];
    
}
/**
 *  加载更多数据
 */
- (void)loadMoreData{
    

    NSString *page = [NSString stringWithFormat:@"%zd",self.page + 1];
    
        LYConditionModel *requestModel = [self getRequestModelWithPage:page];
    
    [LYRequestTool getLocalTeacherAndOrganizationWithcontionModel:requestModel success:^(NSArray<LYPersonalInfo *> *models) {
        
        
        if (models.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.models addObjectsFromArray:models];;
        self.page++;
        [self.tableView reloadData];
        
        
    } failure:^(NSString *code, NSString *message) {
        
        [self.tableView.mj_footer endRefreshing];
    }];
    
    
}

#pragma mark - 上下拉刷新
/**
 *  下拉
 */
- (void)tableViewHeaderRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadNewData];
    
}

/**
 *  上拉
 */
- (void)tableViewfooterRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadMoreData];
    
}





#pragma mark - tableviewDataSource


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.models.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"LYTeacherCell";
    
    LYOrzCell *cell = [LYOrzCell cellWithTableView:tableView andIndetifier:cellID];
    
    LYPersonalInfo *model = self.models[indexPath.section];
    
    cell.model = model;
    
    return cell;
}

#pragma mark - tableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    LYPersonalInfo *model = self.models[indexPath.section];
    LYOrzWebViewController *orzVC = [LYOrzWebViewController orzWebVCWithID:model.user_id];
    [self.navigationController pushViewController:orzVC animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0.1;
    }else{
        return 5;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 90;
}

#pragma mark - 其他
- (LYConditionModel *)getRequestModelWithPage:(NSString *)page{

    LYLocationInfo *locationInfo = [LYDataSourceManager sharedManager].locationInfo;
    
    NSInteger subID = -1;
    if (self.screenBar.subTitle) {
        subID = [LYSubjectListDBTool getIDWithSubTitle:self.screenBar.subTitle];
    }
    
    LYConditionModel *model = [[LYConditionModel alloc] init];
    model.page = page;
    model.per_page = @"10";
    model.distinct = @"10";
    model.role_id = LYRoleTypeOrganization;
    model.token = LYToken;
    
    model.order_type = self.screenBar.orderType;
    model.area_id = [NSString stringWithFormat:@"%ld",(long)self.screenBar.area_id];
    model.subject_path = subID == -1 ? nil : [NSString stringWithFormat:@"%ld",(long)subID];
    model.keyword = self.searchBar.text;
    if (locationInfo) {
        model.lat = locationInfo.lat;
        model.lng = locationInfo.lng;
    }

    return model;
}


- (void)textfieldChange:(UITextField *)searchBar{

    [self loadNewData];
}



-(void)showPickImageAlertView{
    
    __weak typeof(self) weakSelf = self;
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *sortAction = [UIAlertAction actionWithTitle:@"智能排序" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf alertActionWithTitle:@"智能排序" ordertype:LYOrderTypeDefault];
    }];
    
    UIAlertAction *renqiAction = [UIAlertAction actionWithTitle:@"人气最高" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf alertActionWithTitle:@"人气最高" ordertype:LYOrderTypeRenqi];
    }];
    
    UIAlertAction *evaluateAction = [UIAlertAction actionWithTitle:@"评价最高" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf alertActionWithTitle:@"评价最高" ordertype:LYOrderTypeEvaluate];
    }];
    
    UIAlertAction *distanceAction = [UIAlertAction actionWithTitle:@"离我最近" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf alertActionWithTitle:@"离我最近" ordertype:LYOrderTypeNearBy];
    }];
    
    
    UIAlertAction *teacherAction = [UIAlertAction actionWithTitle:@"老师最多" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf alertActionWithTitle:@"离我最近" ordertype:LYOrderTypeNearBy];
    }];
    
    UIAlertAction *evaluateMostAction = [UIAlertAction actionWithTitle:@"评价最多" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf alertActionWithTitle:@"评价最多" ordertype:LYOrderTypeEvaluateMost];
    }];
    
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }];
    
    
    [alertController addAction:sortAction];
    [alertController addAction:renqiAction];
    [alertController addAction:evaluateAction];
    [alertController addAction:distanceAction];
    [alertController addAction:teacherAction];
    [alertController addAction:evaluateMostAction];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}


- (void)alertActionWithTitle:(NSString *)title ordertype:(LYOrderType)orderType {
    
    self.screenBar.sortTitle = title;
    self.screenBar.orderType = orderType;
    [self.tableView.mj_header beginRefreshing];
}


@end
